package cc.dong.dao.impl;

import cc.dong.dao.BaseDao;
import cc.dong.dao.PunchClockDao;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author xiaoDong
 * @version 1.0.0
 * @ClassName PunchClockDaoImpl.java
 * @Description TODO
 * @createTime 2024年05月31日 08:58:00
 */
public class PunchClockDaoImpl extends BaseDao implements PunchClockDao {
    @Override
    public int todayPunching(Integer empId) {
        String sql = "SELECT count(*) count FROM attendance WHERE employeeId = ? AND date = CURDATE()";

        ResultSet resultSet = executeQuery(sql, empId);

        if (resultSet != null) {
            try {
                while (resultSet.next()) {
                   int count = resultSet.getInt("count");
                    return count;
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                closedAll(resultSet);
            }
        }
        return -1;
    }


    @Override
    public int cratePunching(Integer empId) {
        String sql = "INSERT INTO attendance (employeeId,date) VALUES(?,CURDATE())";
        return executeUpdate(sql, empId);

    }

    @Override
    public int updateStartTime(Integer empId) {
        String sql = "UPDATE attendance set startTime =CURTIME() WHERE employeeId = ? AND date = CURDATE()";
        return executeUpdate(sql,empId);
    }

    @Override
    public int updateEndTime(Integer empId) {
        String sql = "UPDATE attendance set endTime =CURTIME() WHERE employeeId = ? AND date = CURDATE()";
        return executeUpdate(sql,empId);
    }
}
